//
//  WLHomeVC.swift
//  WalkerLove
//
//  Created by Z on 2020/4/18.
//  Copyright © 2020 Carlson. All rights reserved.
//

import UIKit
import Alamofire
import SnapKit

let kHomeSectionIdentifyKey = "kHomeSectionIdentifyKey"
let kHomeCellIdentifyKey1 = "kHomeCellIdentifyKey1"
let kHomeCellIdentifyKey2 = "kHomeCellIdentifyKey2"
let kHomeCellIdentifyKey3 = "kHomeCellIdentifyKey3"

let kScreen = UIScreen.main.bounds
enum WLHActivity {
    case banner, opera
}

class WLHomeVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var table: UITableView?
    var activityData = [WLHActivity:Any]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = DeviceTool.ColorFromRGB(0xffffff)
        initSubviews()
        initData()
        
    }
    
    //布局
    func initSubviews() {
        table = UITableView(frame: CGRect.zero, style: .plain)
        table?.separatorStyle = .none
        table?.separatorColor = UIColor.clear
        table?.backgroundColor = UIColor.clear
        table?.delegate = self
        table?.dataSource = self
        table?.tableHeaderView = creatHeaderView()
        table?.register(WLHomeSectionHeaderView.self, forHeaderFooterViewReuseIdentifier: kHomeSectionIdentifyKey)
        table?.register(WLHomeHealthDataCell.self, forCellReuseIdentifier: kHomeCellIdentifyKey1)
        table?.register(WLHomeActivityBannerCell.self, forCellReuseIdentifier: kHomeCellIdentifyKey2)
        table?.register(WLHomeActivityOpearCell.self, forCellReuseIdentifier: kHomeCellIdentifyKey3)
        view.addSubview(table!)
        
        table?.snp.makeConstraints({
            $0.edges.equalToSuperview().offset(0)
        })
    }
    //数据
    func initData() {
        //运营位
        //banner
        requestActivity(type: .banner)
        requestActivity(type: .opera)
    }
    
    func requestActivity(type: WLHActivity) {
        let url = base_url.appending("web/config/os-banner-list")
        var params = ["bannerPosition":"1"]
        if type == .opera {
           params = ["bannerPosition":"2"]
        }
        HttpTool.request(urlStr: url, method: HTTPMethod.get, params: params, completionHandler: { json in
            let arr = json["data"]! as! [AnyObject]
            var models = [String]()
            for j in arr {
                let model = WLHomeActivityModel(JSON: j as! [String : Any])!
                models.append(model.imageUrl!)
            }
            self.activityData[type] = models
            self.table?.reloadData()
        }) { errStr in
            
        }
    }
    
    //私有方法
    func creatHeaderView() -> WLHomeHeaderView {
        let header = WLHomeHeaderView(frame: CGRect(x: 0, y: 0, width: kScreen.width, height: 400))
        return header
    }
    
    //代理
    func numberOfSections(in tableView: UITableView) -> Int {
        if activityData.count>0 {
            return 2
        }
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return activityData.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 104
        }else{
            let idx = activityData.index(activityData.startIndex, offsetBy: indexPath.row)
            let dic = activityData[idx]
            if dic.key == .banner {
                return 160/690.0*(kScreen.width-30)+10
            }else{
                let arr = dic.value as! [String]
                let count = arr.count/2 + arr.count%2
                return 100/167.5*(kScreen.width-40)/2*CGFloat(count) + 20
            }
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sView = tableView.dequeueReusableHeaderFooterView(withIdentifier: kHomeSectionIdentifyKey) as! WLHomeSectionHeaderView
        sView.headerSeationType(type: section)
        return sView
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: UITableViewCell?
        if indexPath.section == 0 {
            cell = tableView.dequeueReusableCell(withIdentifier: kHomeCellIdentifyKey1)
        }else{
            let idx = activityData.index(activityData.startIndex, offsetBy: indexPath.row)
            let obj = activityData[idx]
            if obj.key == .banner {
                cell = tableView.dequeueReusableCell(withIdentifier: kHomeCellIdentifyKey2) as! WLHomeActivityBannerCell
                if let bcell = cell as? WLHomeActivityBannerCell {
                    bcell.datas = obj.value as! [String]
                }
            }else{
                cell = tableView.dequeueReusableCell(withIdentifier: kHomeCellIdentifyKey3)
                if let ocell = cell as? WLHomeActivityOpearCell {
                    ocell.datas = obj.value as! [String]
                }
            }
        }
        cell?.selectionStyle = .none
        return cell!
    }
}
